#!/bin/sh /etc/rc.common
# Copyright (C) 2023 muink
#

. "${IPKG_INSTROOT}/lib/functions.sh"

START=99
USE_PROCD=1

EXTRA_COMMANDS="download_ookla ookla_verify speedtest"
EXTRA_HELP=\
"	download_ookla	Download Ookla Speedtest-CLI
	ookla_verify	Verify Ookla Speedtest-CLI integrity
	speedtest	Start Speedtest Test"

#
OOKLA_SPEEDTEST='/usr/libexec/netspeedtest/speedtest'
SPEEDTEST_RESULT='/var/speedtest_result'
# uci
CONFIG_NAME='netspeedtest'
NAMEDDSECTION='config'



iperf3_instance() {
	procd_open_instance "iperf3"
	procd_set_param command "iperf3"
	procd_append_param command -s
	procd_set_param respawn
	procd_set_param user nobody
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_close_instance
}

librespeed_instance() {
	local action="$1"

	case "$action" in
		start)
			uci set librespeed-go.config.enabled='1'
			uci commit librespeed-go
			/etc/init.d/librespeed-go reload
		;;
		stop)
			uci set librespeed-go.config.enabled='0'
			uci commit librespeed-go
			/etc/init.d/librespeed-go reload
		;;
	esac
}

kill_instance() {
	local instance="$1"
	pgrep -f "$instance" | xargs kill -9 >/dev/null 2>&1
	return 0
}

download_ookla() {
	local url arch=$1
	[ -z "$arch" ] && return 1

	[ "$(uci -q get $CONFIG_NAME.$NAMEDDSECTION.proxy_enabled)" == "1" ] && \
	export ALL_PROXY=$(uci -q get $CONFIG_NAME.$NAMEDDSECTION.proxy_protocol)://$(uci -q get $CONFIG_NAME.$NAMEDDSECTION.proxy_server)

	url=$( \
		curl --connect-timeout 10 --retry 3 -sSL 'https://www.speedtest.net/apps/cli' \
		| grep "Download for Linux" \
		| sed 's|<|\n<|g' \
		| sed -n '/Download for Linux/,/<\/div>/p' \
		| sed -En "s|.*<a href=\"([^\"]+)\">${arch}|\1|p" \
	)
	[ -z "$url" ] && return 1

	[ -n "$url" ] && curl -sSL $url | tar -xvz -C /tmp
	mkdir -p ${OOKLA_SPEEDTEST%/*} 2>/dev/null
	cp -f /tmp/speedtest $OOKLA_SPEEDTEST
	chmod 755 $OOKLA_SPEEDTEST
	ookla_verify || rm -f $OOKLA_SPEEDTEST
	unset ALL_PROXY
}

ookla_verify() {
	if [ -f "$OOKLA_SPEEDTEST" -a -n "$($OOKLA_SPEEDTEST --version 2>/dev/null)" ]; then
		return 0
	else
		return 1
	fi
}

speedtest() {
	local command="speedtest --share"

	if [ "$(uci -q get $CONFIG_NAME.$NAMEDDSECTION.ookla_official)" == "1" ]; then
		ookla_verify
		if [ "$?" == "0" ]; then
			command="$OOKLA_SPEEDTEST --accept-gdpr --accept-license --progress=no"
		else
			uci set $CONFIG_NAME.$NAMEDDSECTION.ookla_official=0
			uci commit $CONFIG_NAME
		fi
	fi

	[ -n "$(pgrep -f "$command")" ] && return 1

	[ "$(uci -q get $CONFIG_NAME.$NAMEDDSECTION.proxy_enabled)" == "1" ] && \
	export ALL_PROXY=$(uci -q get $CONFIG_NAME.$NAMEDDSECTION.proxy_protocol)://$(uci -q get $CONFIG_NAME.$NAMEDDSECTION.proxy_server)

	echo "Testing" > "$SPEEDTEST_RESULT"
	local result=$($command | grep 'www.speedtest.net/result/' | sed -E 's|.+(://www.speedtest.net/\S+).*|https\1|;s|(\.png)$||')
	[ -n "$result" ] && echo "$result" > "$SPEEDTEST_RESULT" || echo "Test failed" > "$SPEEDTEST_RESULT"
}

start_service() {
	local eniperf3 enlibrespeed

	config_load "$CONFIG_NAME"
	eval "eniperf3=\$CONFIG_${NAMEDDSECTION}_iperf3_enabled"
	eval "enlibrespeed=\$CONFIG_${NAMEDDSECTION}_librespeed_enabled"

	[ "$eniperf3" == "1" ] && iperf3_instance
	[ "${enlibrespeed:=0}" == "0" ] && librespeed_instance stop || librespeed_instance start
}

service_triggers() {
	procd_add_reload_trigger "$CONFIG_NAME" 'librespeed-go' 'network'
}

stop_service() {
	uci -q batch <<-EOF
		set $CONFIG_NAME.$NAMEDDSECTION.iperf3_enabled='0'
		set $CONFIG_NAME.$NAMEDDSECTION.librespeed_enabled='0'
		commit $CONFIG_NAME
	EOF
	librespeed_instance stop
}

restart() {
	start
}
